package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import com.example.starseas_phone.Adapters.Apply_ScoreManagelistviewAdapter;
import com.example.starseas_phone.Adapters.ResultQuerylistviewAdapter;
import com.example.starseas_phone.Entity.ScoreManage;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ResultsQueryActivity extends AppCompatActivity {

    private ListView results_query_list;
    private ResultQuerylistviewAdapter adapter;
    private List<ScoreManage> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results_query);
        initid();
        initlist();
    }

    private void initid(){
        results_query_list = (ListView) findViewById(R.id.results_query_list);
        adapter = new ResultQuerylistviewAdapter(ResultsQueryActivity.this,R.layout.results_query_listview,list);
    }

    private void initlist(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Result/GetResultsByToken?token="+Sto.students.getToken())
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        int ResultId = jsonObjects.getInt("ResultId");
                        String StuId= jsonObjects.getString("StuId");
                        String StuName = jsonObjects.getString("StuName");
                        int CourseId= jsonObjects.getInt("CourseId");
                        String CourseName = jsonObjects.getString("CourseName");
                        String WorkerId = jsonObjects.getString("WorkerId");
                        String WorkerName = jsonObjects.getString("WorkerName");
                        String ClassId = jsonObjects.getString("ClassId");
                        String Score=jsonObjects.getString("Score");
                        String OpeningDate=jsonObjects.getString("OpeningDate");
                        int ResultStatus=jsonObjects.getInt("ResultStatus");
                        String ResultNote=jsonObjects.getString("ResultNote");
                        ScoreManage scoreManage = new  ScoreManage(ResultId,StuId,StuName,CourseId,CourseName,WorkerId,WorkerName,ClassId,Score,OpeningDate,ResultStatus,ResultNote);
                        list.add(scoreManage);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            results_query_list.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}